package com.ruoyi.web.recordinfo.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.web.planinfo.entity.Planinfo;
import com.ruoyi.web.planinfo.service.PlaninfoService;
import com.ruoyi.web.recordinfo.entity.Recordinfo;
import com.ruoyi.web.recordinfo.service.RecordinfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 跟进记录表(Recordinfo)表控制层
 *
 */
@RestController
@RequestMapping("recordinfo")
public class RecordinfoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private RecordinfoService recordinfoService;
    @Resource
    private PlaninfoService planinfoService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param recordinfo 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Recordinfo> page, Recordinfo recordinfo) {
        Page<Recordinfo> page1 = this.recordinfoService.page(page, new QueryWrapper<>(recordinfo));
        List<Recordinfo> records = page1.getRecords();
        for (Recordinfo record : records) {
            Planinfo byId = planinfoService.getById(record.getPlanid());
            record.setPlaninfo(byId);
        }
        return success(page1);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.recordinfoService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param recordinfo 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Recordinfo recordinfo) {
        return success(this.recordinfoService.save(recordinfo));
    }

    /**
     * 修改数据
     *
     * @param recordinfo 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Recordinfo recordinfo) {
        return success(this.recordinfoService.updateById(recordinfo));
    }

    /**
     * 删除数据
     *

     * @return 删除结果
     */
    @DeleteMapping
    public R delete(Integer id) {
        return success(this.recordinfoService.removeById(id));
    }
}

